var tmx=null;
var tmy=null;

var lockReconnect = false;
var tt;
let returnData;
let returnData2;
let returnData3;
var flag = true;
//var path = '192.168.129.157:8081/hmmp/';
//var path = '192.168.129.161:8081/hmmp/';
var path = '192.168.129.187:81/';
// "signCode_monitor_type_longtime"
var math = Math.floor(Math.random()*10);
var userCode = "lys_AAA"+math; // 随机数
var websocket;

function createWebSocket(){
    try{
        if ('WebSocket' in window) {
            websocket = new WebSocket("ws://" + path + "wsz?userId=" + userCode+"&&bizType=2");
            init();
        } else if ('MozWebSocket' in window) {
            websocket = new MozWebSocket("ws://" + path + "wsz?userId=" + userCode+"&&bizType=2");
        } else {
            websocket = new SockJS("http://" + path + "wsz/sockjs?userId=" + userCode+"&&bizType=2");
        }
    } catch(e){
        console.log(e);
        reconnect();
    }
}

function reconnect() {
    if(lockReconnect) {
        return;
    };
    lockReconnect = true;
    //没连接上会一直重连，设置延迟避免请求过多
    tt && clearTimeout(tt);
    tt = setTimeout(function () {
        createWebSocket();
        lockReconnect = false;
    }, 4000);
}

function init(){
    websocket.onopen = function(event) {
        console.log("WebSocket:已连接");
        console.log(event);
    };
    websocket.onmessage = function(event) {
        var data = JSON.parse(event.data);
        var type=data.token;
        //console.log(new Date()+"当前用户"+userCode+"WebSocket:收到一条消息-form", data);
        if(type==1){
            var tranAmont=data.data.transamont.payAmount.toFixed(2);
            var tranCount=data.data.transamont.payCount;
            if(flag){
                //初始化
                //交易额
                jyeFlag=initJYE('.jyeint','.jyefloat','.point',{num:tranAmont,len:tranAmont.toString().length});
                //交易数
                init('.dataStatistics',{num:tranCount,len:tranCount.toString().length});
                flag = false;
            }
            //交易额
            if(!jyeFlag.flag){
                //整数
                intdataStatistics('.jyeint',{min:tranAmont,max:tranAmont,time:3000,len:tranAmont.toString().length});
            }else{
                let intnums=tranAmont.toString().substring(0,tranAmont.toString().indexOf('.'));
                let floatnums=tranAmont.toString().substring(tranAmont.toString().indexOf('.')+1,tranAmont.toString().length);
                intdataStatistics('.jyeint',{min:jyeFlag.intnum.num,max:parseInt(intnums),time:3000,len:intnums.length});
                floatdataStatistics('.jyefloat',{min:jyeFlag.floatnum.num,max:parseFloat(floatnums),time:3000,len:floatnums.length});
            }
            //交易数
            dataStatistics('.dataStatistics',{min:tranCount,max:tranCount,time:3000,len:tranCount.toString().length});
            console.log("WebSocket:收到一条消息-form", data);
        }else if(type==2){
            var newOrderParamDto=data.data.newOrderParamDto;
            addTransOrders(newOrderParamDto.param1,newOrderParamDto.param2,newOrderParamDto.param3,newOrderParamDto.param4,newOrderParamDto.param5,newOrderParamDto.param6);
        }else if(type==3){
            var transRuntimeCount=data.data.transRuntimeCount;
            tmx=transRuntimeCount.acqTime;
            tmy=transRuntimeCount.transCount;
            tmx = (new Date(tmx)).getTime();
            // console.log(tmx,tmy);
            // console.log(tmy,tmx);
        }

         console.log("WebSocket:收到一条消息-form", data);

    };
    websocket.onerror = function(event) {
        console.log("WebSocket:发生错误 ");
        console.log(event);
        reconnect();
    };
    websocket.onclose = function(event) {
        console.log("WebSocket:已关闭");
        console.log(event);
        reconnect();
    }
}

createWebSocket();